package com.dididoctor.patient.Activity.Usercentre;

import android.graphics.Color;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.dididoctor.patient.Bean.Token;
import com.dididoctor.patient.MV.EduActivity;
import com.dididoctor.patient.R;
import com.dididoctor.patient.Utils.Util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @version 2.0
 * @类描述：
 * @项目名称：ddgh
 * @类名称： EditPhoneActivity
 * @包名称： com.dididoctor.patient.Activity.Usercentre
 * @创建人： zhanjunjie
 * @创建时间：2016/4/5 16:06
 * @修改人：
 * @修改时间：2016/4/5 16:06
 * @修改备注： new parent class(用一句话描述修改了什么)
 */
public class EditPhoneActivity extends EduActivity implements View.OnFocusChangeListener, EditPhoneView {

    // 本界面
    private EditText mEtPhone, mEtCode;
    private Button mBtnVerification, mBtnConfirm;
    private String phoneNum;
    private EditPhonePresenter presenter;
    private MyCount myCount;

    //EditText文本项集合
    private EditText[] mEdittexts;

    private boolean isStart = false;

    @Override
    public void setContentLayout() {
        setContentView(R.layout.activity_phonenumeditor);

    }

    @Override
    public void init() {
        setText(R.id.tv_title, "更换手机号");

        // 本界面
        mEtPhone = (EditText) findViewById(R.id.et_phone);


        mEtPhone.setOnFocusChangeListener(this);

        mEtCode = (EditText) findViewById(R.id.et_code);
        // TODO 添加监听 1 行
        mEtCode.setOnFocusChangeListener(this);

        mBtnVerification = (Button) findViewById(R.id.btn_verification);
        mBtnVerification.setOnClickListener(this);
        mBtnVerification.setEnabled(false);

        mBtnConfirm = (Button) findViewById(R.id.btn_confirm);
        mBtnConfirm.setOnClickListener(this);
        mBtnConfirm.setEnabled(false);
        presenter = new EditPhonePresenter(this, this);

        //文本集合
        mEdittexts = new EditText[]{mEtPhone, mEtCode};

        //设置文本变化监听器(手机号，验证码，邀请码)
        for (int x = 0; x < mEdittexts.length; x++) {
            setEtInvitTextChangeListener(mEdittexts[x]);
        }

    }


    /**
     * 设置邀请码文本变化监听器
     */
    private void setEtInvitTextChangeListener(EditText mEt) {

        mEt.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                          int arg3) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (mEtCode.getText().length() == 4 && mEtPhone.getText().length() == 11) {
                    mBtnConfirm.setClickable(true);
                    mBtnConfirm.setEnabled(true);
                    mBtnConfirm
                            .setBackgroundResource(R.drawable.btn_home_selector);
                } else {
                    mBtnConfirm.setClickable(false);
                    mBtnConfirm.setEnabled(false);
                    mBtnConfirm
                            .setBackgroundResource(R.drawable.btn_code_normal);
                }

                if (mEtPhone.getText().length() == 11) {
                    mBtnVerification.setClickable(true);
                    mBtnVerification.setEnabled(true);
                    mBtnVerification
                            .setBackgroundResource(R.drawable.btn_home_selector);
                } else {
                    mBtnVerification.setClickable(false);
                    mBtnVerification.setEnabled(false);
                    mBtnVerification
                            .setBackgroundResource(R.drawable.btn_code_normal);
                }

                //讀秒開始後可不能點擊了哦
                if (isStart) {
                    mBtnVerification.setEnabled(false);
                }
            }
        });
    }

    @Override
    public void onClickEvent(View view) {
        switch (view.getId()) {
            case R.id.btn_verification:
                if (checkValue()) {
                    presenter.getInvitationCode(phoneNum);
                }

                break;
            case R.id.btn_confirm:
                if (checkValue()) {
                    presenter.updatephone(phoneNum, mEtCode.getText().toString());
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        switch (v.getId()) {
            case R.id.et_phone:
                mEtPhone.setBackgroundResource(R.drawable.et_login_orange);
                mEtCode.setBackgroundResource(R.drawable.et_login_wihte);
                break;
            case R.id.et_code:
                mEtPhone.setBackgroundResource(R.drawable.et_login_wihte);
                mEtCode.setBackgroundResource(R.drawable.et_login_orange);
                break;
            default:
                break;
        }

    }


    public boolean checkValue() {
        phoneNum = mEtPhone.getText().toString().trim();
        if (Util.isEmpty(phoneNum)) {
            showToastMessage("手机号不能为空");
            return false;
        }

        if (!isMobileNo(phoneNum)) {
            showToastMessage("请输入正确格式的手机号");
            return false;
        }

        return true;
    }

    public static boolean isMobileNo(String mobiles) {
        Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }


    public class MyCount extends CountDownTimer {
        public MyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
            isStart = true;
        }

        @Override
        public void onFinish() {
            mBtnVerification.setText("获取验证码");
            // sendCode.setBackgroundResource(R.drawable.date_btn_no);
            // sendCode.setTextColor(Color.parseColor("#00923f"));
            mBtnVerification.setEnabled(true);
            isStart = false;
        }

        @Override
        public void onTick(long millisUntilFinished) {
            mBtnVerification.setText(millisUntilFinished / 1000 + "秒");
            mBtnVerification.setTextColor(Color.parseColor("#ffffff"));
        }
    }

    @Override
    public void updateSucced() {
        showToastMessage("修改成功");
        Token.setPhone(phoneNum);
        finish();
    }

    @Override
    public void updatefail() {
        showToastMessage("请求错误");
    }

    @Override
    public void getInvitationCode() {
        mBtnVerification.setEnabled(false);
        myCount = new MyCount(60000, 1000);
        myCount.start();// 请求服务器发送验证码
    }

    @Override
    public void getInvitationCodeFail() {
        showToastMessage("获取失败");
    }

}
